#include <stdio.h>

void printArray(int** dp,int n)
{
    for(int i=2;i<=n;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d\t",dp[i][j]);
        }
        printf("\n");
    }
}

int main(){
    int n;
    scanf("%d",&n);
    int* data=new int[n];
    int i=0,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%d",&data[i]);
    }

    //dp[i][j] 从j开始像后数i个的最大得分
    int ** dp=new int*[n+1];
    for(i=0;i<=n;i++)
    {
        dp[i]=new int[n];
    }
    
    for(i=0;i<n;i++)
    {
        dp[0][i]=0;
        dp[1][i]=0;
    }

    for(i=2;i<=n;i++)
    {
        for(j=0;j<n;j++)
        {
            int maxScore=-1;
            for(k=1;k<i;k++)
            {
                int center=(j+k)%n;
                int end=(j+i)%n;
                int score=data[j]*data[center]*data[end]+dp[k][j]+dp[i-k][center];
                if(maxScore<score)
                {
                    maxScore=score;
                }
            }
            dp[i][j]=maxScore;
        }
    }

    // printArray(dp,n);
    int maxScore=-1;
    for(i=0;i<n;i++)
    {
        if(maxScore<dp[n][i])
        {
            maxScore=dp[n][i];
        }
    }
    printf("%d\n",maxScore);
    
    for(i=0;i<=n;i++)
    {
        delete dp[i];
    }
    delete dp;
    
    delete data;
}